package com.zhaoyue.study.sort;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 2017/2/24
 * Time: 14:39
 * 排序算法精练
 */
public class SortDemo {

    /**
     * 冒泡排序
     * @param nums
     */
    public static void maopaoSort(int[] nums) {
        int size = nums.length;
        int temp;
        for (int i = 0; i < size - 1; i++) {
            for (int j = i + 1; j < size; j++) {
                if(nums[i] > nums[j]){
                    temp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = temp;
                }
            }
        }
    }


    /**
     * 快速排序
     *
     * @param nums
     * @param start
     * @param end
     */
    public static void quickSort(int[] nums, int start, int end) {
        //
        if (start < end) {
            int base = nums[start];//选择的基准值，选择第一个
            int temp;//记录临时中间值
            int i = start, j = end;
            do {
                while (nums[i] < base && i < end) {
                    i++;
                }
                while (nums[j] > base && j > start) {
                    j--;
                }
                if (i <= j) {
                    temp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = temp;
                    i++;
                    j--;
                }

            } while (i <= j);
            if (start < j) {
                quickSort(nums, start, j);
            }
            if (end > i) {
                quickSort(nums, i, end);
            }
        }

    }


    public static void main(String[] args) {


        int[] nums = new int[]{8, 5, 4, 7, 9, 1, 2, 10, 12, 29, 15};
        //SortDemo.quickSort(nums, 0, nums.length - 1);
        SortDemo.maopaoSort(nums);


        for (int i = 0; i < nums.length; i++) {
            System.out.println(nums[i]);
        }

    }

}
